<? 

define('E', substr($_SERVER['SCRIPT_FILENAME'], 0, strripos($_SERVER['SCRIPT_FILENAME'],'/') + 1));

require_once E.'_lib/_const.php';
require_once E.'_lib/_db.php';
require_once E.'_lib/_lib.php';

$d = (int)datafetch('value', D_SITEWIDE, 0, 'watchers');

$vol = 30;

$intervals = array(
	mktime(7, 0, 0, 9, 20, 2011) => array('min' => 0, 'max' => 0),
	mktime(7, 15, 0, 9, 20, 2011) => array('min' => 682, 'max' => 1217),
	mktime(7, 30, 0, 9, 20, 2011) => array('min' => 657, 'max' => 1352),
	mktime(7, 45, 0, 9, 20, 2011) => array('min' => 0, 'max' => 0),
);

$now = mktime();
$left = $right = 0;

foreach($intervals as $i => $v) if ($now < $i) { $right = $i; break; } else $left = $i;

if ($left && $right) {

	$l = $left ? $intervals[$left] : array('min' => 0, 'max' => 0);
	$r = $right ? $intervals[$right] : array('min' => 0, 'max' => 0);

	$min = $l['min'] + ($r['min'] - $l['min']) * ($now - $left) / ($right - $left);
	$max = $l['max'] + ($r['max'] - $l['max']) * ($now - $left) / ($right - $left);
	
	$d += mt_rand(-$vol, $vol);
	
	if ($d < $min) $d = $min;
	if ($d > $max) $d = $max;

	dataset('db_update', tb('data'), array('value' => (int)$d), "id_data=".D_SITEWIDE." AND name='watchers'");
	
} else 
	if ($d)
		dataset('db_update', tb('data'), array('value' => 0), "id_data=".D_SITEWIDE." AND name='watchers'");
